﻿using Microsoft.Extensions.Caching.Memory;
using OA.Model;
using OA.Model.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OA.BLL
{
    public class CEmployee : CBase
    {
        public static Employee GetEmployeeByID(int id,List<Employee> list)
        {
            Employee info = list.Where(m => m.ID == id).FirstOrDefault();
            if (info == null) { info = new Employee() { EmpName = "" }; }
            return info;
        } 

        public static Employee GetEmployeeByEmpID(string empID, List<Employee> list)
        {
            Employee info = list.Where(m => m.EmpID == empID).FirstOrDefault();
            if (info == null) { info = new Employee() { EmpName = "" }; }
            return info;
        }

        public static Employee GetEmployeeByPositionID(int positionID, List<VEmployeePosition> list)
        {
            Employee employee = new Employee();
            VEmployeePosition info = list.Where(m => m.PositionID == positionID && m.EmployeeStatus != -1 && m.Status != -1).FirstOrDefault();
            if(info==null)
            {
                info = list.Where(m => m.PositionID == positionID && m.Status != -1).FirstOrDefault();//人离职了，离职之前需要委托工作。管理员也可以委托离职人员工作。
            }
            if (info != null) { employee.EmpID = info.EmpID; employee.EmpName = info.EmpName; employee.Pic = info.Pic; employee.Status = info.Status; }
            return employee;
        }


        public static void InintEmployeeTree(MyTree tree, int departmentID, List<SimpleDepartment2> listDepartment, List<Employee8> listEmployee, List<string> selects)
        {
            var employees = listEmployee.Where(m => m.DepartmentID == departmentID).ToList();
            foreach (Employee8 employee in employees)
            {
                MyTree employeePoint = new MyTree();
                employeePoint.t = employee.EmpName;
                employeePoint.v = employee.EmpID;
                if (selects.Contains(employee.EmpID))
                {
                    employeePoint.s = 1;
                }
                employeePoint.d = new List<MyTree>();
                tree.d.Add(employeePoint);
            }
            var children = listDepartment.Where(m => m.ParentDepartmentID == departmentID).ToList();
            foreach (var department in children)
            {
                MyTree departmentPoint = new MyTree();
                departmentPoint.t = department.Name;
                departmentPoint.v = "";
                departmentPoint.d = new List<MyTree>();
                InintEmployeeTree(departmentPoint, department.ID, listDepartment, listEmployee, selects);
                tree.d.Add(departmentPoint);
            }
        }


        public static void AutoCountersignEmployee(List<CountersignEmployee> list, IMemoryCache _cache)
        {
            CacheHelper cacheHelper = new CacheHelper(_cache);
            List<Department> listDepartment = cacheHelper.GetDepartments();
            List<Position> listPosition = cacheHelper.GetPositions();
            List<Employee> listEmployee = cacheHelper.GetEmployees();

            foreach (CountersignEmployee item in list)
            {
                Position itemPosition = listPosition.Where(m => m.ID == item.PositionID).FirstOrDefault();
                if (itemPosition != null)
                {
                    item.PositionName = itemPosition.Name;
                    Department infoDepartment = listDepartment.Where(m => m.ID == itemPosition.DepartmentID).FirstOrDefault();
                    if (infoDepartment != null)
                    {
                        item.DepatrmentName = infoDepartment.Name;
                    }

                }
                Employee itemEmployee = listEmployee.Where(m => m.EmpID == item.EmpID).FirstOrDefault();
                if (itemEmployee != null)
                {
                    item.EmpName = itemEmployee.EmpName;
                }
            }

        }

    }
}
